******
* author: Anthony Harding (tony.harding@gatech.edu)
* Georgia Institute of Technology, School of Public Policy


* This script generates regression coefficients and tables

* 1st Imports Data
* 2nd Runs baseline regressions
******


clear all
set mem 1G
set matsize 10000
set maxvar 10000

* Set working directory
cd "\\Client\C$\Users\aharding6\Dropbox (GaTech)\Tobetransferred\ConsistentClimateConvergence - Copy\Replication"




*-------------------------------------------------------------------------------
*-------------------------------------------------------------------------------
* 1. Import and prep data
*-------------------------------------------------------------------------------
*-------------------------------------------------------------------------------

*-------------------------------------------------------------------------------
* Step 1 - Import main data
*-------------------------------------------------------------------------------
* Import main dataset (source Burke et al. 2015) - contains temperature and growth variables
use data/input/GrowthClimateDataset, clear

*-------------------------------------------------------------------------------
* End Step 1 - End Step 1 - End Step 1 - End Step 1 - End Step 1 - End Step 1 
*-------------------------------------------------------------------------------


*-------------------------------------------------------------------------------
* Step 2 - Generate new useful variables
*-------------------------------------------------------------------------------
* Generate country indicator and xtset data
xtset iso_id year



* Generate new economic variables
qui gen poor = (GDPpctile_WDIppp<50)
qui replace poor=. if GDPpctile_WDIppp==.
qui gen log_gdpCAP_wdi = log(gdpCAP_wdi)
qui gen loggdpCAP_wdi_l = l.log_gdpCAP_wdi
qui gen growthWDI_l = l.growthWDI
qui gen poor_growthWDI_l = poor*growthWDI_l

* Generate new weather variables
qui gen UDel_temp_popweight_2 = UDel_temp_popweight^2
gen poor_temp = poor*UDel_temp_popweight
gen poor_prec = poor*UDel_precip_popweight
gen poor_temp2 = poor*UDel_temp_popweight_2
gen poor_prec2 = poor*UDel_precip_popweight_2
gen dtemp = UDel_temp_popweight-l.UDel_temp_popweight
gen dprec = UDel_precip_popweight-l.UDel_precip_popweight
gen dtemp2 = UDel_temp_popweight_2-l.UDel_temp_popweight_2
gen dprec2 = UDel_precip_popweight_2-l.UDel_precip_popweight_2
gen poor_dtemp = poor*dtemp
gen poor_dprec = poor*dprec
gen poor_dtemp2 = poor*dtemp2
gen poor_dprec2 = poor*dprec2

* Generate new year variables
gen timegr = time/(1+time)
gen timegr2 = 1/2*time^2/(1+time)
*gen time2 = time^2
gen year2 = year^2
gen yeargr = year/(1+year)

*-------------------------------------------------------------------------------
* End Step 2 - End Step 2 - End Step 2 - End Step 2 - End Step 2 - End Step 2 
*-------------------------------------------------------------------------------

*-------------------------------------------------------------------------------
* Step 3 - Rename and relabel key variables
*-------------------------------------------------------------------------------
* Rename variables
ren UDel_temp_popweight temp
ren UDel_temp_popweight_2 temp2
ren UDel_precip_popweight prec
ren UDel_precip_popweight_2 prec2

* Relabel variables
label variable temp "Temp"
label variable temp2 "Temp Sq."
label variable prec "Precip"
label variable prec2 "Precip Sq."
label variable dtemp "$\Delta$Temp"
label variable dtemp2 "$\Delta$Temp Sq."
label variable dprec "$\Delta$Precip"
label variable dprec2 "$\Delta$Precip Sq."
label variable poor "poor"
label variable poor_temp "Temp*Poor"
label variable poor_prec "Precip*Poor"
label variable poor_temp2 "Temp Sq.*Poor"
label variable poor_prec2 "Precip Sq.*Poor"
*-------------------------------------------------------------------------------
* End Step 3 - End Step 3 - End Step 3 - End Step 3 - End Step 3 - End Step 3 
*-------------------------------------------------------------------------------


*-------------------------------------------------------------------------------
* Step 4 - Export prepped dataset
*-------------------------------------------------------------------------------
* Save as .dta file
save "data/output/PreppedDataset", replace

preserve
keep year iso iso_id temp prec growthWDI gdpCAP_wdi loggdpCAP_wdi_l TotGDP growthWDI_l
export delimited using "data/output/mainDataset.csv", replace
restore
*-------------------------------------------------------------------------------
* End Step 4 - End Step 4 - End Step 4 - End Step 4 - End Step 4 - End Step 4 
*-------------------------------------------------------------------------------



*-------------------------------------------------------------------------------
*-------------------------------------------------------------------------------
* 2. Run and Store Regressions
*-------------------------------------------------------------------------------
*-------------------------------------------------------------------------------


*-------------------------------------------------------------------------------
* Step 1 - Pooled SR, no country time trend, no GDP lag
*-------------------------------------------------------------------------------
*** Column 1 ***
eststo: reghdfe growthWDI temp temp2 prec prec2, absorb(i.year  i.iso_id) vce(cluster iso_id)
estadd local TimeTrend "None", replace
estadd local RichPoor "Pooled", replace
estadd local MaxTemp=round(_b[temp]/(-2*_b[temp2]),0.1), replace

*-------------------------------------------------------------------------------
* End Step 1 - End Step 1 - End Step 1 - End Step 1 - End Step 1 - End Step 1 
*-------------------------------------------------------------------------------

*-------------------------------------------------------------------------------
* Step 2 - Pooled SR, linear country time trend, no GDP lag
*-------------------------------------------------------------------------------
*** Column 2 ***
eststo: reghdfe growthWDI temp temp2 prec prec2, absorb(i.year  i.iso_id i.iso_id#c.time) vce(cluster iso_id)
estadd local TimeTrend "Linear", replace
estadd local RichPoor "Pooled", replace
estadd local MaxTemp=round(_b[temp]/(-2*_b[temp2]),0.1), replace

*-------------------------------------------------------------------------------
* End Step 2 - End Step 2 - End Step 2 - End Step 2 - End Step 2 - End Step 2 
*-------------------------------------------------------------------------------


*-------------------------------------------------------------------------------
* Step 3 - Pooled SR, quadratic country time trend, no GDP lag
*-------------------------------------------------------------------------------
*** Column 3 ***
eststo: reghdfe growthWDI temp temp2 prec prec2, absorb(i.year  i.iso_id i.iso_id#c.time i.iso_id#c.time2) vce(cluster iso_id)
estadd local TimeTrend "Quadratic", replace
estadd local RichPoor "Pooled", replace
estadd local MaxTemp=round(_b[temp]/(-2*_b[temp2]),0.1), replace

*-------------------------------------------------------------------------------
* End Step 3 - End Step 3 - End Step 3 - End Step 3 - End Step 3 - End Step 3 
*-------------------------------------------------------------------------------

*-------------------------------------------------------------------------------
* Step 4 - Pooled SR, no country time trend, 1 GDP lag
*-------------------------------------------------------------------------------
*** Column 4 ***
eststo: ivreghdfe growthWDI temp temp2 prec prec2 (loggdpCAP_wdi_l = l3.log_gdpCAP_wdi), absorb(i.year  i.iso_id)  cluster(iso_id)
estadd local TimeTrend "None", replace
estadd local RichPoor "Pooled", replace
estadd local MaxTemp=round(_b[temp]/(-2*_b[temp2]),0.1), replace

*-------------------------------------------------------------------------------
* End Step 4 - End Step 4 - End Step 4 - End Step 4 - End Step 4 - End Step 4 
*-------------------------------------------------------------------------------


*-------------------------------------------------------------------------------
* Step 5 - Pooled SR, linear country time trend, 1 GDP lag
*-------------------------------------------------------------------------------
*** Column 5 ***
eststo: ivreghdfe growthWDI temp temp2 prec prec2 (loggdpCAP_wdi_l = l3.log_gdpCAP_wdi), absorb(i.year  i.iso_id i.iso_id#c.time) cluster(iso_id)
estadd local TimeTrend "Linear", replace
estadd local RichPoor "Pooled", replace
estadd local MaxTemp=round(_b[temp]/(-2*_b[temp2]),0.1), replace

*-------------------------------------------------------------------------------
* End Step 5 - End Step 5 - End Step 5 - End Step 5 - End Step 5 - End Step 5
*-------------------------------------------------------------------------------


*-------------------------------------------------------------------------------
* Step 6 - Pooled LR, convergent country time trend, 1 GDP lag
*-------------------------------------------------------------------------------
*** Column 6 ***
eststo: ivreghdfe growthWDI dtemp dtemp2 dprec dprec2 temp temp2 prec prec2 (growthWDI_l = l3.growthWDI), absorb(i.year  i.iso_id) cluster(iso_id)
estadd local TimeTrend "None", replace
estadd local RichPoor "Pooled", replace
estadd local MaxTemp=round(_b[dtemp]/(-2*_b[dtemp2]),0.1), replace

*-------------------------------------------------------------------------------
* End Step 6 - End Step 6 - End Step 6 - End Step 6 - End Step 6 - End Step 6
*-------------------------------------------------------------------------------



*-------------------------------------------------------------------------------
* Step 7 - Rich/poor LR, convergent country time trend, 1 GDP lag
*-------------------------------------------------------------------------------
*** Column 7 ***
eststo: ivreghdfe growthWDI dtemp dtemp2 dprec dprec2 temp temp2 prec prec2 poor_dtemp poor_dtemp2 poor_dprec poor_dprec2 poor_temp poor_temp2 poor_prec poor_prec2 (growthWDI_l poor_growthWDI_l = i.poor#c.l3.growthWDI), absorb(i.year  i.iso_id) cluster(iso_id)
estadd local TimeTrend "None", replace
estadd local RichPoor "Rich/Poor", replace
estadd local MaxTemp=round(_b[dtemp]/(-2*_b[dtemp2]),0.1), replace

*-------------------------------------------------------------------------------
* End Step 7 - End Step 7 - End Step 7 - End Step 7 - End Step 7 - End Step 7
*-------------------------------------------------------------------------------



*-------------------------------------------------------------------------------
* Step 8 - Export Regression Coefficients
*-------------------------------------------------------------------------------
* Export coefficients for use in projections
esttab using "data/output/RegressionCoeff_Projections.csv", drop(_cons) not noobs plain replace

* Clear results
eststo clear

*-------------------------------------------------------------------------------
* End Step 8 - End Step 8 - End Step 8 - End Step 8 - End Step 8 - End Step 8
*-------------------------------------------------------------------------------

